// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   ServletWriter.java

package com.iphaeton.lms.course.scorm12.runtime.client;

import com.iphaeton.lms.course.scorm12.DebugIndicator;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class ServletWriter
{

    public ServletWriter()
    {
    }

    public static ObjectInputStream postObjects(URL servlet, Serializable objs[], String sessionid)
        throws Exception
    {
        if(_Debug)
            System.out.println("In ServletWriter::postObjects()");
        URLConnection con;
        try
        {
            if(_Debug)
                System.out.println("Opening HTTP URL connection to servlet.");
            con = servlet.openConnection();
        }
        catch(Exception e)
        {
            if(_Debug)
                System.out.println("Exception caught In ServletWriter::postObjects()");
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw e;
        }
        if(_Debug)
        {
            System.out.println("HTTP connection to servlet is open");
            System.out.println("configuring HTTP connection properties");
        }
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false);
        con.setRequestProperty("Content-Type", "text/plain");
        con.setAllowUserInteraction(false);
        con.setRequestProperty("Cookie", sessionid);
        ObjectOutputStream out;
        try
        {
            if(_Debug)
                System.out.println("Creating new http output stream");
            out = new ObjectOutputStream(con.getOutputStream());
            if(_Debug)
            {
                System.out.println("Created new http output stream.");
                System.out.println("Writing command and data to servlet...");
            }
            int numObjects = objs.length;
            if(_Debug)
                System.out.println("Num objects: " + numObjects);
            for(int x = 0; x < numObjects; x++)
            {
                out.writeObject(objs[x]);
                if(_Debug)
                    System.out.println("just wrote a serialized object on output stream... " + objs[x].getClass().getName());
            }

        }
        catch(Exception e)
        {
            if(_Debug)
            {
                System.out.println("Exception caught In ServletWriter::postObjects()");
                System.out.println(e.getMessage());
            }
            e.printStackTrace();
            throw e;
        }
        try
        {
            if(_Debug)
                System.out.println("Flushing Object Output Stream.");
            out.flush();
        }
        catch(IOException ioe)
        {
            if(_Debug)
                System.out.println("Caught IOException when calling out.flush()");
            ioe.printStackTrace();
            throw ioe;
        }
        catch(Exception e)
        {
            if(_Debug)
                System.out.println("Caught Exception when calling out.flush()");
            e.printStackTrace();
            throw e;
        }
        try
        {
            if(_Debug)
                System.out.println("Closing Object Output Stream.");
            out.close();
        }
        catch(IOException ioe)
        {
            if(_Debug)
            {
                System.out.println("Caught IOException when calling out.close()");
                System.out.println(ioe.getMessage());
            }
            ioe.printStackTrace();
            throw ioe;
        }
        catch(Exception e)
        {
            if(_Debug)
                System.out.println("Caught Exception when calling out.close()");
            e.printStackTrace();
            throw e;
        }
        ObjectInputStream in;
        try
        {
            if(_Debug)
                System.out.println("Creating new http input stream.");
            in = new ObjectInputStream(con.getInputStream());
        }
        catch(Exception e)
        {
            if(_Debug)
            {
                System.out.println("Exception caught In ServletWriter::postObjects()");
                System.out.println(e.getMessage());
            }
            e.printStackTrace();
            throw e;
        }
        return in;
    }

    private static boolean _Debug;

    static 
    {
        _Debug = DebugIndicator.ON;
    }
}
